package com.zxtyshangjia.zxtyjms.first.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zxtyshangjia.zxtyjms.R;
import com.zxtyshangjia.zxtyjms.SetActivity;
import com.zxtyshangjia.zxtyjms.base.BaseFragment;
import com.zxtyshangjia.zxtyjms.first.bean.FirstBean;
import com.zxtyshangjia.zxtyjms.utils.Api;
import com.zxtyshangjia.zxtyjms.utils.SpUtils;
import com.zxtyshangjia.zxtyjms.utils.ToastUtil;
import com.zxtyshangjia.zxtyjms.utils.network.HttpUtil;
import com.zxtyshangjia.zxtyjms.utils.network.PostCallBack;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by lv .
 * 首页
 */

public class OneFragment extends BaseFragment {
    private View oneView;

    /**
     * 设置
     */
    private TextView mSet;

    /**
     * 加盟商名称
     */
    private TextView mName;

    /**
     * 地点 哪个地区的加盟商
     */
    private TextView mAddress;

    /**
     * 总收益
     */
    private TextView mSumIncome;

    /**
     * 昨日商家增长量
     */
    private TextView mBsIncrease;

    /**
     * 昨日收益
     */
    private TextView mIncomeIncrease;

    /**
     * 加盟商id
     */
    private String e_id;

    //首页信息回调
    private PostCallBack firstBack = new PostCallBack() {
        @Override
        public void onSuccess(Object data) {

            if(data != null && data instanceof FirstBean){
                FirstBean bean = (FirstBean) data;
                if(bean.flag.equals("success")){

                    //获取信息成功 显示到页面上
                    mName.setText(bean.data.account+" ");
                    mAddress.setText(bean.data.area_name);
                    mBsIncrease.setText(bean.data.recommend_count);
                    mIncomeIncrease.setText(bean.data.yesterday_price);
                    mSumIncome.setText(bean.data.deduct_price);

                    //保存得到的用户账号
                    SpUtils.getInstance(mActivity).save("account",bean.data.account);

                }else {
                    ToastUtil.showToast(bean.message);
                }
            }

        }

        @Override
        public void onError(int code, String msg) {

        }
    };



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (oneView == null) {
            oneView = View.inflate(mActivity, R.layout.fragment_one, null);
        }
        e_id = SpUtils.getInstance(mActivity).getString("e_id","");
        bindViews();
        bindListener();
        initData();
        return oneView;
    }

    private void bindViews() {
        //在此绑定view
        mSet = (TextView) oneView.findViewById(R.id.first_set_tv);
        mName = (TextView) oneView.findViewById(R.id.jms_name_tv);
        mAddress = (TextView) oneView.findViewById(R.id.jms_address_tv);
        mSumIncome = (TextView) oneView.findViewById(R.id.income_sum_tv);
        mBsIncrease = (TextView) oneView.findViewById(R.id.business_increase_tv);
        mIncomeIncrease = (TextView) oneView.findViewById(R.id.income_increase_tv);

    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view.getId() == mSet.getId()){
                //跳转设置页面
                Intent intent = new Intent(mActivity, SetActivity.class);
                mActivity.startActivity(intent);
            }
        }
    };

    private void bindListener() {
        //在此绑定监听器
        mSet.setOnClickListener(mClickListener);
    }

    private void initData() {
        //在此处理数据

        Map<String,String> map = new HashMap<>();
        map.put("e_id",e_id);
        new HttpUtil(Api.FIEST_URL,map, FirstBean.class,firstBack).postExecute();

    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onClick(View view) {

    }
}
